package com.singing.manager;

import com.singing.dao.JudgeDao;
import com.singing.dao.SingScoreDao;
import com.singing.dao.SingerDao;
import com.singing.model.Command;
import com.singing.model.Judge;
import com.singing.model.SingScore;
import com.singing.model.Singer;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SingScoreManager extends BaseManager {

    // 列表
    private static final int LIST = 1;


    // 添加
    private static final int ADD = 2;


    // 查询单个歌手的详细信息
    private static final int SINGLE = 3;


    // 查询多个歌手的信息
    private static final int ALL = 4;


    SingerDao singerDao = new SingerDao();

    JudgeDao judgeDao = new JudgeDao();

    SingScoreDao singScoreDao = new SingScoreDao();

    public SingScoreManager(String title, List<Command> commandList) {
        super(title, commandList);
    }

    @Override
    protected int processCommand(int command) {
        switch (command) {
            case LIST:
                break;
            case ADD:
                showGiveScore();
                break;
            case SINGLE:
                showSingleSingerScore();
                break;
            case ALL:
                showAllSingerScore();
        }
        return command;
    }

    private void showGiveScore() {
        Scanner scanner = new Scanner(System.in);
        Integer id = null;
        Singer singer = null;
        while (true) {
            showSingerList();
            System.out.println("请输入歌手的id");
            try {
                String idStr = scanner.next();
                id = Integer.parseInt(idStr);
                singer = singerDao.selectById(id);
                if (singer == null) {
                    throw new RuntimeException("歌手不存在,清重新输入");
                }
                break;
            } catch (NumberFormatException e) {
                System.err.println("输入有误，请重新输入");
            } catch (RuntimeException e) {
                System.err.println(e.getMessage());
            }
        }


        showSingleSinger(singer);

        if (singScoreDao.exists(singer.getId())) {
            System.err.println("该歌手已经评分，请重新选择");
            return;
        }

        List<Judge> judges = judgeDao.selectAll();
        List<SingScore> singScores = new ArrayList<>();
        System.out.println("共有" + judges.size() + "个评委为上述选手进行评分");

        for (Judge judge : judges) {
            SingScore singScore = new SingScore();
            singScore.setSingerId(id);
            singScore.setJudgeId(judge.getId());
            Integer score = null;
            while (true) {
                System.out.println(String.format("裁判【%s】进行评分,请在下方输入分数（0-100）:", judge.getName()));
                String scoreStr = scanner.next();
                try {
                    score = Integer.parseInt(scoreStr);
                    if (score < 0 || score > 100) {
                        throw new NumberFormatException();
                    }
                    break;
                } catch (NumberFormatException e) {
                    System.err.println("分数格式不正确，请重新输入");
                }

            }
            singScore.setScore(score);
            singScores.add(singScore);
        }

        singScoreDao.insertList(singScores);
    }

    private void showSingerList() {
        System.out.println("歌手id\t\t歌手名称\t\t歌手性别\t\t是否已评分");
        List<Singer> singers = singerDao.selectAll();
        List<Integer> alreadyIds = singScoreDao.selectAllSingerId();
        for (Singer singer : singers) {

            if (alreadyIds.contains(singer.getId())) {
                System.err.println(singer.getId() + "\t\t" + singer.getName() + "\t\t" + (singer.getGender() == 0 ? "男" : "女") + "\t\t" + (alreadyIds.contains(singer.getId()) ? "已评分" : "未评分"));

            } else {
                System.out.println(singer.getId() + "\t\t" + singer.getName() + "\t\t" + (singer.getGender() == 0 ? "男" : "女") + "\t\t" + (alreadyIds.contains(singer.getId()) ? "已评分" : "未评分"));

            }
        }
        System.out.println("");
    }

    private void showSingleSinger(Integer id) {
        Singer singer = singerDao.selectById(id);
        System.out.println("歌手id\t\t歌手名称\t\t歌手性别");
        System.out.println(singer.getId() + "\t\t" + singer.getName() + "\t\t" + (singer.getGender() == 0 ? "男" : "女"));
        System.out.println("");

    }

    private void showSingleSinger(Singer singer) {
        System.out.println("歌手id\t\t歌手名称\t\t歌手性别");
        System.out.println(singer.getId() + "\t\t" + singer.getName() + "\t\t" + (singer.getGender() == 0 ? "男" : "女"));
        System.out.println("");

    }


    private void showSingleSingerScore() {
        ArrayList<Command> commands = new ArrayList<>();
        commands.add(new Command(1, "输入歌手ID"));
        new SingleSingerScoreManager("查询单个歌手评分", commands).manage();
    }

    private void showAllSingerScore() {
        ArrayList<Command> commands = new ArrayList<>();
        commands.add(new Command(1, "重新加载"));
        new AllSingerScoreManager("查询所有歌手评分", commands).manage();
    }
}
